﻿/*
	Copyright 2010 Intuitive Solutions

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

using System;
using System.Configuration;
using System.Collections.Generic;

namespace Crochet.Configuration
{
	public class CrochetConfiguration : ConfigurationSection
	{
		private static CrochetConfiguration mcInstance = null;

		protected CrochetConfiguration()
		{
		}

		public static CrochetConfiguration Instance
		{
			get
			{
				if (mcInstance == null)
				{
					mcInstance = (CrochetConfiguration)(ConfigurationManager.GetSection("crochet"));
				}

				return mcInstance;
			}
		}

		[ConfigurationProperty("immediate", IsKey = false, IsRequired = false)]
		public ImmediateSchedulerCollectionElement ImmediateSchedulers
		{
			get
			{
				return (ImmediateSchedulerCollectionElement)base["immediate"];
			}
			set
			{
				base["immediate"] = value;
			}
		}

		[ConfigurationProperty("threadPool", IsKey = false, IsRequired = false)]
		public ThreadPoolSchedulerCollectionElement ThreadPoolSchedulers
		{
			get
			{
				return (ThreadPoolSchedulerCollectionElement)base["threadPool"];
			}
			set
			{
				base["threadPool"] = value;
			}
		}

		[ConfigurationProperty("executionPool", IsKey = false, IsRequired = false)]
		public ExecutionPoolSchedulerCollectionElement ExecutionPoolSchedulers
		{
			get
			{
				return (ExecutionPoolSchedulerCollectionElement)base["executionPool"];
			}
			set
			{
				base["executionPool"] = value;
			}
		}

		[ConfigurationProperty("timer", IsKey = false, IsRequired = false)]
		public TimerSchedulerCollectionElement TimerSchedulers
		{
			get
			{
				return (TimerSchedulerCollectionElement)base["timer"];
			}
			set
			{
				base["timer"] = value;
			}
		}

		[ConfigurationProperty("rescheduleOnException", IsKey = false, IsRequired = false)]
		public RescheduleOnExceptionCollectionElement RescheduleOnExceptionPolicies
		{
			get
			{
				return (RescheduleOnExceptionCollectionElement)base["rescheduleOnException"];
			}
			set
			{
				base["rescheduleOnException"] = value;
			}
		}

		[ConfigurationProperty("retryOnExceptionPolicy", IsKey = false, IsRequired = false)]
		public RetryOnExceptionCollectionElement RetryOnExceptionPolicies
		{
			get
			{
				return (RetryOnExceptionCollectionElement)base["retryOnExceptionPolicy"];
			}
			set
			{
				base["retryOnExceptionPolicy"] = value;
			}
		}

		public CrochetConfigurationElement GetPolicyElement(string sPolicyName)
		{
			CrochetConfigurationElement cceCrochetConfigurationElement = null;

			if (sPolicyName != null &&
				!string.Empty.Equals(sPolicyName.Trim()))
			{
				cceCrochetConfigurationElement = RetryOnExceptionPolicies[sPolicyName];

				if (cceCrochetConfigurationElement == null)
				{
					cceCrochetConfigurationElement = RescheduleOnExceptionPolicies[sPolicyName];
				}
			}

			return cceCrochetConfigurationElement;
		}
	}
}
